import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { loadJS } from "./utils";
import authConfig from "./config";
var GoogleAuth = /*#__PURE__*/ function _target() {
    "use strict";
    function GoogleAuth() {
        var _this = this;
        _class_call_check(this, GoogleAuth);
        _define_property(this, "load", window.google ? true : false);
        _define_property(this, "getCode", function() {
            return new Promise(function(resolve, reject) {
                var _this_client, _this_client_requestCode;
                /** google     */ _this.resolve = function(data) {
                    resolve(_object_spread({
                        code: data.code,
                        state: ""
                    }, data));
                };
                _this.reject = function(data) {
                    reject(data);
                };
                _this.reject = reject;
                if (!_this.client) {
                    _this.init().then(function() {
                        var _this_client;
                        (_this_client = _this.client) === null || _this_client === void 0 ? void 0 : _this_client.requestCode();
                        return;
                    });
                }
                (_this_client = _this.client) === null || _this_client === void 0 ? void 0 : (_this_client_requestCode = _this_client.requestCode) === null || _this_client_requestCode === void 0 ? void 0 : _this_client_requestCode.call(_this_client);
            });
        });
        _define_property(this, "getAccessToken", function() {
            return new Promise(function(resolve, reject) {
                try {
                    _this.getCode().then(function(res) {
                        var code = res.code;
                    });
                } catch (err) {
                    reject(err);
                }
            });
        });
        if ((_instanceof(this, GoogleAuth) ? this.constructor : void 0) !== GoogleAuth) {
            return;
        }
        if (!GoogleAuth._instance) {
            GoogleAuth._instance = this;
        }
        /** 加载sdk */ this.loadSdk();
        return GoogleAuth._instance;
    }
    _create_class(GoogleAuth, [
        {
            key: "loadSdk",
            value: function loadSdk(cb, onError) {
                var _this = this;
                loadJS("https://cdn.nfkings.com/js/sdks/accounts.google.com_gsi_client.js", function() {
                    var _window_google, _this1, _this_onLoad;
                    _this.onLoaded();
                    window.GOOGLE = _this.client = (_window_google = window.google) === null || _window_google === void 0 ? void 0 : _window_google.accounts.oauth2.initCodeClient({
                        client_id: authConfig.GOOGLE_CLIENTID,
                        ux_mode: "popup",
                        state: authConfig.REDIRECT_URI,
                        // https://www.googleapis.com/auth/userinfo.email 邮箱 OpenID openID
                        scope: "https://www.googleapis.com/auth/userinfo.profile            https://www.googleapis.com/auth/userinfo.email  openid",
                        callback: function(tokenResponse) {
                            var _this1, _this_resolve;
                            if (tokenResponse.error) {
                                var _this2, _this_reject;
                                (_this_reject = (_this2 = _this).reject) === null || _this_reject === void 0 ? void 0 : _this_reject.call(_this2, tokenResponse);
                                _this.reject = undefined;
                                return;
                            }
                            (_this_resolve = (_this1 = _this).resolve) === null || _this_resolve === void 0 ? void 0 : _this_resolve.call(_this1, tokenResponse);
                            _this.resolve = undefined;
                        },
                        // redirect_uri: REDIRECT_URI,
                        error_callback: function(error) {
                            var _this1, _this_reject;
                            (_this_reject = (_this1 = _this).reject) === null || _this_reject === void 0 ? void 0 : _this_reject.call(_this1, error);
                            _this.reject = undefined;
                        }
                    });
                    _this.load = true;
                    (_this_onLoad = (_this1 = _this).onLoad) === null || _this_onLoad === void 0 ? void 0 : _this_onLoad.call(_this1);
                    console.log(_this.client);
                    cb === null || cb === void 0 ? void 0 : cb();
                }, function() {
                    var _this1, _this_onError;
                    onError === null || onError === void 0 ? void 0 : onError({
                        msg: "google sdk load fail,please try later"
                    });
                    (_this_onError = (_this1 = _this).onError) === null || _this_onError === void 0 ? void 0 : _this_onError.call(_this1);
                });
            }
        },
        {
            key: "init",
            value: function init() {
                var _this = this;
                /** 假如 sdk 加载失败可以使用init 重新加载sdk */ return new Promise(function(resolve, reject) {
                    if (_this.load && window.google) {
                        resolve(GoogleAuth._instance);
                        return;
                    }
                    _this.loadSdk(function() {
                        resolve(GoogleAuth._instance);
                    }, reject);
                });
            }
        },
        {
            key: "onLoaded",
            value: function onLoaded(cb) {
                cb === null || cb === void 0 ? void 0 : cb();
            }
        }
    ], [
        {
            key: "getInstance",
            value: function getInstance() {
                if (!GoogleAuth._instance) {
                    GoogleAuth._instance = new GoogleAuth();
                }
                return GoogleAuth._instance;
            }
        }
    ]);
    return GoogleAuth;
}();
_define_property(GoogleAuth, "_instance", null);
export default GoogleAuth.getInstance();

 //# sourceMappingURL=google.js.map